package com.ruoyi.food.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.environment.domain.OpmEnvRule;
import com.ruoyi.food.domain.OpmFoodSafeResp;
import com.ruoyi.food.mapper.OpmFoodSafeRespMapper;
import com.ruoyi.food.service.IOpmFoodSafeRespService;
import com.ruoyi.system.api.LinkFileServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 食品安全应急预案Service业务层处理
 *
 * @author cjie
 * @date 2021-07-24
 */
@Service
public class OpmFoodSafeRespServiceImpl extends LinkFileServiceImpl implements IOpmFoodSafeRespService {
    @Autowired
    private OpmFoodSafeRespMapper opmFoodSafeRespMapper;

    /**
     * 查询食品安全应急预案
     *
     * @param id 食品安全应急预案ID
     * @return 食品安全应急预案
     */
    @Override
    public OpmFoodSafeResp selectOpmFoodSafeRespById(Long id) {
        OpmFoodSafeResp result = opmFoodSafeRespMapper.selectOpmFoodSafeRespById(id);
        result.setFileList(getFileList(id.toString()));
        return result;

    }

    /**
     * 查询食品安全应急预案列表
     *
     * @param opmFoodSafeResp 食品安全应急预案
     * @return 食品安全应急预案
     */
    @Override
    public List<OpmFoodSafeResp> selectOpmFoodSafeRespList(OpmFoodSafeResp opmFoodSafeResp) {
        return opmFoodSafeRespMapper.selectOpmFoodSafeRespList(opmFoodSafeResp);
    }

    /**
     * 新增食品安全应急预案
     *
     * @param opmFoodSafeResp 食品安全应急预案
     * @return 结果
     */
    @Override
    public int insertOpmFoodSafeResp(OpmFoodSafeResp opmFoodSafeResp) {
        opmFoodSafeResp.setId(IdUtil.getSnowflake(0, 0).nextId());
        opmFoodSafeResp.setCreateTime(DateUtils.getNowDate());
        opmFoodSafeResp.setCreateBy(SecurityUtils.getUsername());
        linkFile(opmFoodSafeResp.getFileIdList(), opmFoodSafeResp.getId().toString());
        return opmFoodSafeRespMapper.insertOpmFoodSafeResp(opmFoodSafeResp);
    }

    /**
     * 修改食品安全应急预案
     *
     * @param opmFoodSafeResp 食品安全应急预案
     * @return 结果
     */
    @Override
    public int updateOpmFoodSafeResp(OpmFoodSafeResp opmFoodSafeResp) {
        opmFoodSafeResp.setUpdateTime(DateUtils.getNowDate());
        opmFoodSafeResp.setUpdateBy(SecurityUtils.getUsername());
        linkFile(opmFoodSafeResp.getFileIdList(), opmFoodSafeResp.getId().toString());
        return opmFoodSafeRespMapper.updateOpmFoodSafeResp(opmFoodSafeResp);
    }

    /**
     * 批量删除食品安全应急预案
     *
     * @param ids 需要删除的食品安全应急预案ID
     * @return 结果
     */
    @Override
    public int deleteOpmFoodSafeRespByIds(Long[] ids) {
        String[] res = new String[ids.length];
        for(int i=0;i<ids.length;i++){
            res[i] = String.valueOf(ids[i]);
        }
        deleteLinkFile(res);
        return opmFoodSafeRespMapper.deleteOpmFoodSafeRespByIds(ids);
    }

    /**
     * 删除食品安全应急预案信息
     *
     * @param id 食品安全应急预案ID
     * @return 结果
     */
    @Override
    public int deleteOpmFoodSafeRespById(Long id) {
        return opmFoodSafeRespMapper.deleteOpmFoodSafeRespById(id);
    }
}
